package easy;

/**
 * @author admin
 * 167. 两数之和 II - 输入有序数组
 * 解题思路：双指针法，定义一个res保存两数之和，在满足做左指针在右指针左边的条件下
 * 一开始res为最左边的数加最右边的数，与target进行比较，若比target大，则将右指针
 * 左移，反之将左指针右移。倘若res等于target，返回当前的left+1和right+1
 */
public class LeeCode167 {

    public int[] twoSum(int[] numbers, int target) {

        int left = 0;
        int right = numbers.length - 1;
        while (left <= right){
            int res = numbers[left] + numbers[right];
            if (res == target){
                return new int[]{left+1,right+1};
            }else if (res > target){
                right--;
            }else {
                left++;
            }
        }
        return new int[]{};
    }

    public static void main(String[] args) {
        System.out.println(new LeeCode167().twoSum(new int[]{2,7,11,15,16},9));
    }
}
